﻿namespace mok5;
class Program
{
    public static void Main(string[] args)
    {
        double usd = 23.73;
        int vnd = UsdToVnd(usd);
        // Console.WriteLine($"${usd} USD = {vnd:C} VND");
        // Console.WriteLine($"${vnd} VND = {VndToUsd(vnd):C} USD");

        // Console.WriteLine($"there are snakes at the zoo");
        // Console.WriteLine($"{ReverseWord("there are snakes at the zoo")}");
        string[] words = { "racecar", "talented", "deified", "tent", "tenet" };

        // Console.WriteLine("Is it a palindrome?");
        foreach (string word in words)
        {
            // Console.WriteLine($"{word}: {IsPalindrome(word)}");
        }


        // int target = 30;
        // int[] coins = [5, 5, 50, 25, 25, 10, 5];
        // int[,] result = TwoCoins(coins, target);
        // if (result.Length == 0) 
        // {
        //     Console.WriteLine("No two coins make change");
        // } 
        // else
        // {
        //     for (var i = 0; i < result.GetLength(0); i++)
        //     {
        //         if (result[i, 0] == -1)
        //         {
        //             break;
        //         }
        //         Console.WriteLine($"Change found at positions {result[i,0]} and {result[i,1]}");
        //
        //     }
        //
        // }


        //练习 - 完成挑战，添加使游戏可玩的方法
        Console.WriteLine("Would you like to play? (Y/N)");
        if (ShouldPlay(Console.ReadLine()))
        {
            PlayGame();
        }
    }

    static Random random = new Random();

    private static void PlayGame()
    {
        bool play = true;

        while (play)
        {
            int target = random.Next(1, 5);
            int roll = random.Next(1, 6);

            Console.WriteLine($"Roll a number greater than {target} to win!");
            Console.WriteLine($"You rolled a {roll}");
            Console.WriteLine(WinOrLose(target, roll));
            Console.WriteLine("\nPlay again? (Y/N)");
            play = ShouldPlay(Console.ReadLine());
        }
    }

    //方法应确定玩家是赢还是输
    private static string WinOrLose(int target, int roll)
    {
        if (target > roll)
        {
            return "You Lose!";
        }
        else
        {
            return "You Win!";
        }
    }

    //此方法应检索用户输入并确定用户是否要再次玩游戏   
    private static bool ShouldPlay(string s)
    {
        if (s.Equals("Y") || s.Equals("y"))
        {
            return true;
        }
        else if (s.Equals("N") || s.Equals("n"))
        {
            return false;
        }
        else
        {
            return false;
        }
    }


    static int[,] TwoCoins(int[] coins, int target)
    {
        int[,] result = { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 } };
        int count = 0;
        for (int i = 0; i < coins.Length; i++)
        {
            for (int j = 1; j < coins.Length; j++)
            {
                if (coins[i] + coins[j] == target)
                {
                    result[count, 0] = i;
                    result[count, 1] = j;
                    count++;
                }

                if (count == result.GetLength(0))
                {
                    return result;
                }
            }
        }

        return count == 0 ? new int[0, 0] : result;
    }

    static bool IsPalindrome(string word)
    {
        int start = 0;
        int end = word.Length - 1;
        while (start < end)
        {
            if (word[start] == word[end])
            {
                return true;
            }

            start++;
            end--;
        }

        return false;
    }

    static int UsdToVnd(double usd)
    {
        return (int)usd * 45646;
    }

    static double VndToUsd(int vnd)
    {
        double rate = 23500;
        return vnd / rate;
    }

    static string ReverseWord(string word)
    {
        string result = "";
        string[] split = word.Split(" ");
        foreach (string s in split)
        {
            string temp = s + " ";
            for (int i = temp.Length - 1; i >= 0; i--)
            {
                result += temp[i];
            }
        }

        return result.Trim();
    }
}